<?php
require_once("../../../config.inc.php");
require_once("../../../app/_inc/include.php");
require_once(SERVER_FS_ROOT."/app/util/imex/_inc/importExport.func.php");

$errMsg = sxREQ('msg');
$objUser = $_SESSION[USEROBJ];
$aDataArea = $objUser->getDataArea();
$dataArea = sxREQUEST('da');
if(sxREQ("reqtype") == "export") {
	$errMsg .= exportRel();		
}

if($dataArea==''){
	$url = $_SESSION[USEROBJ]->getHistory(-1, true)."&msg=".urlencode(_t("L'area dati non @ definita."));
	serverRedir($url);
}
if(!isset($aDataArea[$dataArea])){
	$url = $_SESSION[USEROBJ]->getHistory(-1, true)."&msg=".
		urlencode(_t("L'utente non @ qualificato per vedere l'area dati '%S'",$dataArea));
	serverRedir($url);
}

//carico le relationship 
$sSQLRP = "SELECT r.*, p.description AS parent, p.indef_article AS parent_indef_article, " .
		" c.description AS child, c.indef_article AS child_indef_article " .
		" FROM relationship r " .
		"INNER JOIN entity p ON p.entity = r.parent_entity " .
		"INNER JOIN entity c ON c.entity = r.child_entity " .
		"WHERE r.parent_entity LIKE '$dataArea%'  ORDER BY p.description, r.number, c.description";
$objSDAO->query($sSQLRP,"relPar");

//output HTML
$aOpz = array("tit2"=>_t("Lista relazioni padre-figlio"));
echo(headerHTML($aOpz));
echo(bodyHTML($aOpz));
echo("<table width=\"100%\" cellpadding=\"03\" cellspacing=\"0\">\n<tr>\n<td>\n");
//output comandi
$aCmd = array(
					"export"=>array("pag"=>"javascript:document.objFrm.submit();",
										"title"=>_t("Esporta relazion1"),
										"text"=>_t("esporta"),
										"perm"=>"M"),
					"newRel"=>array("pag"=>"../rela/relationshipIns.php?da=$dataArea&xsn=".XSN,
										"title"=>_t("Nuova relreqtype"),
										"text"=>_t("nuova relreqtype"),
										"perm"=>"M"),
					"back"=>array("pag"=>$objUser->getHistory(-1,false),
										"title"=>$objUser->getHistoryText(-1),
										"text"=>$objUser->getHistoryText(-1),
										"perm"=>"R")
					);
echo(commandsHTML($aCmd));	

//relazioni
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");

echo "<form action='".$_SERVER['PHP_SELF']."' name='objFrm' method='POST'>\n";
echo "<input type='hidden' name='xsn' value='".XSN."'>\n";
echo "<input type='hidden' name='da' value='".$dataArea."'>\n";
echo "<input type='hidden' name='reqtype' value='export'>\n";

echo("<tr>" .
			"<td class=\"titl01\" colspan='5'>"._t("Relazioni padre-figlio area dati '%s'",$dataArea)."</td>" .
		"</tr>\n");
echo "<tr>" ;
echo		"<td class=\"tabh01\" width='92%'>"._t("Relreqtype")."</td>" ;
echo		"<td class=\"tabh01\" width='2%'>n.</td>" ;
echo		"<td class=\"tabh01\" width='2%'>IC</td>" ;
echo		"<td class=\"tabh01\" width='2%'>CD</td>" ;
echo		"<td class=\"tabh01\" width='2%'>exp.</td>" ;
echo "</tr>\n";

//output relationship: entity=parent
$oldEnt = '';
$nn=0;
while($row = $objSDAO->fetch("relPar")) {
	if ($oldEnt != $row["parent_entity"]) {
		$oldEnt = $row["parent_entity"];
		echo "<tr>\n<td class=\"text01\" colspan='5'>";		
		echo $row["parent_indef_article"];
		echo " <a class=\"text01\" ".goToPag("../enty/entityView.php?da=$dataArea&ent="
				.urlencode($row["parent_entity"])."&xsn=".XSN)." title=\""._t("Visualizza entity padre")."\">";
		echo '<b>'.$row["parent"].'</b></a>';		
		echo "</td>\n</tr>\n";		
	}
	echo "<tr>\n<td class=\"text01\">";		
	echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	echo ($row["children_optionality"]=='Y')?' sometimes ':' ';		
	echo "<a class=\"text01\" ".goToPag("../rela/relationshipMod.php?da=$dataArea&ent="
			.urlencode($row["parent_entity"])."&entityChi=".urlencode($row["child_entity"])
			."&number=".$row["number"]."&xsn=".XSN)." title=\""._t("Visualizza la relreqtype")." "
			.$row["number"]."\">";
	echo "<b>".$row["description_parent_child"]."</b>";		
	echo "</a>";
	echo (($row["children_multeplicity"]=='Y')?"&nbsp;one or more than one " :"&nbsp;one");		
	echo "<a class=\"text01\" ".goToPag("../enty/entityView.php?da=$dataArea&ent="
			.urlencode($row["child_entity"])."&xsn=".XSN)." title=\""._t("View child entity")."\">";
	echo '<b>'.$row["child"].'</b></a>';		
	echo "</td>\n";
	echo "<td class=\"text01\">".$row["number"]."</td>\n";
	echo "<td class=\"text01\">".$row["integrity_check"]."</td>\n";
	echo "<td class=\"text01\">".$row["cascade_delete"]."</td>\n";
	$nn++;
	echo "<td class=\"text01\">".
		"<input type='checkbox' name='cbexp_$nn' value=\"".
		$row['parent_entity'].'@'.$row['child_entity'].'@'.$row['number'].
		"\" class='text01'></td></tr>\n";
}
echo("</table>");
echo(commandsHTML($aCmd));
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());

function exportRel() {
	global $objUser;
	$aReqExp = array();
 	foreach($_REQUEST as $key=>$value) {
 		if(substr($key,0,6) == "cbexp_") {
  			$aReqExp[] = array('relationship',$value);
  			$objUser->setHistoryVar(array($key=>''));
 		}
 	}
  	$objUser->setHistoryVar(array('reqtype'=>''));
	if (count($aReqExp) == 0) {
		return _t("Nessun elemento selezionato");
	}
	$errMsg = tabExport($aReqExp);
	return $errMsg;
}
?>